﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Combres;
using Infrastructure.CrossCutting.IoC;
using Presentation.Web.UI.Attibutes;
using Presentation.Web.UI.BootStrapper;
using Presentation.Web.UI.ViewModels;
using StackExchange.Profiling;

namespace Presentation.Web.UI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
           // filters.Add(new LanguageAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            RouteTable.Routes.AddCombresRoute("Combres");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                   "PrettyDetails",
                   "{Id}",
                       new { controller = "Dinners", action = "Details" },
                       new { Id = @"\d+" }
                   );

            routes.MapRoute(
                    "UpcomingDinners",
                    "Dinners/Page/{page}",
                    new { controller = "Dinners", action = "Index" }
            );

            routes.MapRoute("DefaultLocalized",
               "{controller}/{action}/{id}",
               new { controller = "Home", action = "Index", id = UrlParameter.Optional }
           );

            routes.MapRoute(
               "OpenIdDiscover",
               "Auth/Discover");
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            IoCFactory.SetCurrent(new IoCFactoryImpl());
            IBootStrapper bootStrapper = new DefaultBootStrapper(IoCFactory.GetContainer());
            bootStrapper.Boot();

            Version version = Assembly.GetExecutingAssembly().GetName().Version;
            Application["Version"] = string.Format("{0}.{1}", version.Major, version.Minor);


            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
        public override void Init()
        {
            this.PostAuthenticateRequest += new EventHandler(MvcApplication_PostAuthenticateRequest);
            this.BeginRequest += new EventHandler(MvcApplication_BeginRequest);
            this.EndRequest += new EventHandler(MvcApplication_EndRequest);
            base.Init();
        }
        void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
        {
            HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                string encTicket = authCookie.Value;
                if (!String.IsNullOrEmpty(encTicket))
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encTicket);
                    var id = new WebIdentity(ticket);
                    HttpContext.Current.User = new GenericPrincipal(id, null);
                }
            }
        }
        void MvcApplication_BeginRequest(object sender, EventArgs e)
        {
            if (Request.IsLocal)
            {
                MiniProfiler.Start();
            }
        }


        void MvcApplication_EndRequest(object sender, EventArgs e)
        {
            MiniProfiler.Stop();
        }
    }
}